package com.huluobo.day11_im.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huluobo.day11_im.IMApp;
import com.huluobo.day11_im.R;
import com.huluobo.day11_im.chat.view.MyChatActivity;
import com.huluobo.day11_im.adapter.ConversationListAdapter;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyConversationFragment extends Fragment {
    private List<EMConversation> conversations;

    private RecyclerView recyclerView;
    private ConversationListAdapter conversationListAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_my_conversation,
                container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.conversation_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        conversationListAdapter = new ConversationListAdapter(R.layout.item_conversation,
                conversations);
        recyclerView.setAdapter(conversationListAdapter);
        EMClient.getInstance().chatManager().addMessageListener(new EMMessageListener() {
            @Override
            public void onMessageReceived(List<EMMessage> messages) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadConversations();
                    }
                });
            }
        });
        conversationListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view,
                                    int position) {
                String conversationId =
                        conversations.get(position).conversationId();
                int chatType = 0;
                switch (conversations.get(position).getType()) {
                    case Chat:
                        chatType = 1;
                        break;
                    case GroupChat:
                        chatType = 2;
                        break;
                }
                Intent intent = new Intent(getActivity(), MyChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(IMApp.CHAT_CONVERSATION_ID, conversationId);
                bundle.putInt(IMApp.CHAT_TYPE, chatType);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });

        loadConversations();

    }

    @Override
    public void onResume() {
        super.onResume();
        loadConversations();
    }

    private void loadConversations() {
        Map<String, EMConversation> emConversationMap =
                EMClient.getInstance().chatManager().getAllConversations();
        conversations = new ArrayList<>(emConversationMap.values());
        conversationListAdapter.notifyDataSetChanged();
    }
}
